package flc.ast.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.stark.calculator.unit.constant.ConvertType;
import com.stark.calculator.unit.model.UnitBean;
import com.stark.calculator.unit.model.UnitDataProvider;

import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityAreaBinding;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.Str2NumUtil;

public class AreaActivity extends BaseAc<ActivityAreaBinding> {

    String str = "1.0";
    private UnitBean mBaseUnitBean, newUnitbean;
    private List<UnitBean> mAllUnitBean;
    private double mInputValue = 1;


    @Override
    protected int onCreate() {
        return R.layout.activity_area;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(this, mDataBinding.event5Container);
        mDataBinding.ivAreaBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivAreaClear.setOnClickListener(this);
        mDataBinding.ivAreaDian.setOnClickListener(this);
        mDataBinding.ivAreaNum0.setOnClickListener(this);
        mDataBinding.ivAreaNum1.setOnClickListener(this);
        mDataBinding.ivAreaNum2.setOnClickListener(this);
        mDataBinding.ivAreaNum3.setOnClickListener(this);
        mDataBinding.ivAreaNum4.setOnClickListener(this);
        mDataBinding.ivAreaNum5.setOnClickListener(this);
        mDataBinding.ivAreaNum6.setOnClickListener(this);
        mDataBinding.ivAreaNum7.setOnClickListener(this);
        mDataBinding.ivAreaNum8.setOnClickListener(this);
        mDataBinding.ivAreaNum9.setOnClickListener(this);
        mDataBinding.llAreaMore1.setOnClickListener(this);
        mDataBinding.llAreaMore2.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivAreaClear:
                str = "";
                mDataBinding.tvAreaNum1.setText(str);
                mDataBinding.tvAreaNum2.setText(str);
                break;
            case R.id.ivAreaNum0:
                if (str.length() == 0) {
                    return;
                }
                str += "0";
                mDataBinding.tvAreaNum1.setText(str);
                mInputValue = Str2NumUtil.parse(str);
                updateNewUnitView();
                break;
            case R.id.ivAreaNum1:
                str += "1";
                mDataBinding.tvAreaNum1.setText(str);
                mInputValue = Str2NumUtil.parse(str);
                updateNewUnitView();
                break;
            case R.id.ivAreaNum2:
                str += "2";
                mDataBinding.tvAreaNum1.setText(str);
                mInputValue = Str2NumUtil.parse(str);
                updateNewUnitView();
                break;
            case R.id.ivAreaNum3:
                str += "3";
                mDataBinding.tvAreaNum1.setText(str);
                mInputValue = Str2NumUtil.parse(str);
                updateNewUnitView();
                break;
            case R.id.ivAreaNum4:
                str += "4";
                mDataBinding.tvAreaNum1.setText(str);
                mInputValue = Str2NumUtil.parse(str);
                updateNewUnitView();
                break;
            case R.id.ivAreaNum5:
                str += "5";
                mDataBinding.tvAreaNum1.setText(str);
                mInputValue = Str2NumUtil.parse(str);
                updateNewUnitView();
                break;
            case R.id.ivAreaNum6:
                str += "6";
                mDataBinding.tvAreaNum1.setText(str);
                mInputValue = Str2NumUtil.parse(str);
                updateNewUnitView();
                break;
            case R.id.ivAreaNum7:
                str += "7";
                mDataBinding.tvAreaNum1.setText(str);
                mInputValue = Str2NumUtil.parse(str);
                updateNewUnitView();
                break;
            case R.id.ivAreaNum8:
                str += "8";
                mDataBinding.tvAreaNum1.setText(str);
                mInputValue = Str2NumUtil.parse(str);
                updateNewUnitView();
                break;
            case R.id.ivAreaNum9:
                str += "9";
                mDataBinding.tvAreaNum1.setText(str);
                mInputValue = Str2NumUtil.parse(str);
                updateNewUnitView();
                break;
            case R.id.ivAreaDian:
                if (str.contains(".")){
                    return;
                }
                str += ".";
                mDataBinding.tvAreaNum1.setText(str);
                mInputValue = Str2NumUtil.parse(str);
                updateNewUnitView();
                break;
            case R.id.llAreaMore1:
                Intent intent = new Intent(AreaActivity.this, SelLengthActivity.class);
                intent.putExtra("selName", mDataBinding.tvAreaType1.getText().toString());
                SelLengthActivity.convertType= ConvertType.AREA;
                startActivityForResult(intent, 11);
                break;
            case R.id.llAreaMore2:
                Intent intent1 = new Intent(AreaActivity.this, SelLengthActivity.class);
                intent1.putExtra("selName", mDataBinding.tvAreaType2.getText().toString());
                SelLengthActivity.convertType=ConvertType.AREA;
                startActivityForResult(intent1, 12);
                break;
        }
    }

    @Override
    protected void initData() {
        updateBaseUnitView();
        updateNewUnitView();
    }

    private void updateBaseUnitView() {
        if (mBaseUnitBean == null) {
            mBaseUnitBean = UnitDataProvider.getBaseUnitBean(ConvertType.AREA);
        }
        str=mBaseUnitBean.value;
        mDataBinding.tvAreaType1.setText(mBaseUnitBean.cnName);
        mDataBinding.tvAreaNum1.setText(mBaseUnitBean.value);
        mDataBinding.tvAreaUnit1.setText(mBaseUnitBean.enName);
    }

    private void updateNewUnitView() {
        if (mAllUnitBean == null) {
            mAllUnitBean = UnitDataProvider.getDatas(ConvertType.AREA);
        }
        for (UnitBean bean : mAllUnitBean) {
            if (bean.cnName.equals(mDataBinding.tvAreaType2.getText().toString())) {
                double newValue = ((1 * bean.baseRateValue) / mBaseUnitBean.baseRateValue) * mInputValue;
                bean.value = String.valueOf(newValue);
                mDataBinding.tvAreaType2.setText(bean.cnName);
                mDataBinding.tvAreaNum2.setText(bean.value);
                mDataBinding.tvAreaUnit2.setText(bean.enName);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 11) {
                String selName = data.getStringExtra("selName");
                for (UnitBean bean : mAllUnitBean) {
                    if (bean.cnName.equals(selName)) {
                        mBaseUnitBean = bean;
                        mBaseUnitBean.value = String.valueOf(1.0);
                    }
                }
                mInputValue=1;
                updateBaseUnitView();
                updateNewUnitView();
            } else if (requestCode == 12) {
                String selName = data.getStringExtra("selName");
                mDataBinding.tvAreaType2.setText(selName);
                updateNewUnitView();
            }
        }
    }
}